package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.senEmail.TestMail;
import com.spider.queue.SpiderBlockQueue;
import com.spider.task.SendEmailTask;
import com.spider.util.SpiderCondition;

public class SendEmails implements Runnable {
	private static StringBuffer sb = new StringBuffer();
	static{
    	try {
    		URL txt = TestMail.class.getClassLoader().getResource(SpiderCondition.EMAIL_CONTENT_PATH);
    		File file = new File(txt.getPath());
    		BufferedReader reader = new BufferedReader(new FileReader(file));
			String tmp = "";
			while((tmp=reader.readLine())!=null){
    			sb.append(tmp);
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/** 从队列中取得游记那地址， 发送邮件
	 */
	public void send(){
		ExecutorService spiderPool = Executors.newFixedThreadPool(4);//SpiderExecuterService.getExecutePool();
		while(true){
			spiderPool.execute(new SendEmailTask(sb.toString()));
			if(SpiderBlockQueue.emailQueue.size()==0){
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void run() {
		send();
	}
}

